﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Code.Models;
using System.Text.RegularExpressions;

namespace Code.BLL.Arithmetic
{
    internal class BJKL8:LotteryTypeProduct
    {
        public override PlayTypeProduct GetPlayType(Models.PlayType_E playType)
        {
            PlayTypeProduct product;
            switch (playType)
            {
                case PlayType_E.ChoseNum:
                    product = new ChoseNum(regChoseNum);
                    break;
                case PlayType_E.LongFu:
                    product = new LongFu(regLongFu);
                    break;
                case PlayType_E.HeZhi:
                    product = new HeZhi(regHeZhi);
                    break;
                default:
                    throw new ArgumentException("快乐8没有找到当前玩法:" + playType);
            }
            product.lotteryType = LotteryType.BJKL8;
            return product;
        }
        private Regex regChoseNum = new Regex(@"^(?![1-9a-e]{0,}(?<num>[1-9a-e])[1-9a-e]{0,}\k<num>)[1-9a-e]{1,10}$", RegexOptions.Compiled);
        private Regex regLongFu = new Regex(@"^(?![龙虎和]{0,}(?<num>[龙虎和])[龙虎和]{0,}\k<num>)[龙虎和]{1,3}$");
        private Regex regHeZhi  = new Regex(@"^(?![大小单双]{0,}(?<num>[大小单双])[大小单双]{0,}\k<num>)[大小单双]{1,4}$", RegexOptions.Compiled);
    }
    /// <summary>
    /// 快乐选号
    /// </summary>
    internal class ChoseNum : PlayTypeProduct
    {
        private PlayType_E PlayType { get; set; }
        private string _OpenCode;
        private Regex reg;
        public ChoseNum(Regex regDouble)
            : base(regDouble)
        {
            PlayType = PlayType_E.ChoseNum;
            reg = regDouble;
        }

        protected override IEnumerable<string> _GetWinNums(string userChoseNumber, string openCode)
        {
            if (openCode == null)
                throw new ArgumentNullException("openCode", "参数不能为空");
            _OpenCode = PlayType.GetOpenCode(openCode, base.lotteryType);
            if (userChoseNumber == null)
                throw new ArgumentNullException("userChoseNumber", "参数不能为空");
            var openCodeList = openCode.Split(',');
            if (openCodeList.Length != 20)
                throw new ArgumentException("开奖号码格式不正确:"+openCode);

            userChoseNumber = _RegistNum(userChoseNumber);//判断用户选择的号码是否合格
            if (userChoseNumber == null)
                throw new ArgumentException("参数格式不正确" + userChoseNumber, "userChoseNumber");

            List<string> winNums = new List<string>();
            foreach (var item in userChoseNumber.Split(','))
            {
                if (openCodeList.Contains(item))
                    winNums.Add(item);
            }
            return winNums;
        }

        protected override int _GetNumCount(string userChoseNumber)
        {
            if (string.IsNullOrEmpty(userChoseNumber))
                return 0;
            return 1;
        }

        protected override List<string> _GetChoseNumberList(string userChoseNumber)
        {
            if (string.IsNullOrEmpty(userChoseNumber))
                return new List<string>();
            return new List<string> { userChoseNumber};
        }
        protected override string _RegistNum(string userChoseNumber)
        {
            if (userChoseNumber == null || userChoseNumber == "")
                return null;
            var chars = new string[] {"a","","b","c","d","e" };
            var i = 0;
            var number = string.Join("", userChoseNumber.Split(',').Select(p => {
                if (p.StartsWith("0"))
                    p = p[1].ToString();
                else if (p.Length == 2)
                {
                    p = chars[i];
                    i++;
                }
                return p; }).ToArray());

            if (reg.IsMatch(number))
                return userChoseNumber;
            else
                return null;
        }
    }
    /// <summary>
    /// 和值玩法
    /// </summary>
    internal class HeZhi : PlayTypeProduct
    {
        private PlayType_E playType;
        public HeZhi(Regex regDouble) : base(regDouble) { playType = PlayType_E.HeZhi; }
        protected override IEnumerable<string> _GetWinNums(string userChoseNumber, string openCode)
        {
            if (string.IsNullOrEmpty(openCode))
                throw new ArgumentNullException("openCode", "参数不能为空");
            var _openCode = playType.GetOpenCode(openCode, base.lotteryType);
            if (string.IsNullOrEmpty(userChoseNumber))
                throw new ArgumentNullException("userChoseNumber", "参数不能为空");
            if (base._RegistNum(userChoseNumber) == null)
                throw new ArgumentException("参数格式不正确:"+userChoseNumber, "userChoseNumber");
            var sum = 0;
            openCode.Split(',').Each(p => {
                sum += int.Parse(p);
            });
            var str1 = sum > 810 ? "大" : "小";
            var str2 = sum % 2 == 0 ? "双" : "单";
            var winList = new List<string>();
            if (userChoseNumber.Contains(str1))
                winList.Add(str1);
            if (userChoseNumber.Contains(str2))
                winList.Add(str2);
            return winList;
        }

        protected override int _GetNumCount(string userChoseNumber)
        {
            if (string.IsNullOrEmpty(userChoseNumber))
                return 0;
            else
                return userChoseNumber.Length;
        }

        protected override List<string> _GetChoseNumberList(string userChoseNumber)
        {
            if (string.IsNullOrEmpty(userChoseNumber))
                return new List<string>();
            return userChoseNumber.Select(p => p.ToString()).ToList();
        }
    }
    /// <summary>
    /// 龙虎玩法
    /// </summary>
    internal class LongFu : PlayTypeProduct
    {
        private PlayType_E playType;
        public LongFu(Regex regDouble) : base(regDouble) { playType = PlayType_E.LongFu; }
        protected override IEnumerable<string> _GetWinNums(string userChoseNumber, string openCode)
        {
            if (string.IsNullOrEmpty(openCode))
                throw new ArgumentNullException("openCode", "参数不能为空");
            var _openCode = playType.GetOpenCode(openCode, base.lotteryType);
            if (string.IsNullOrEmpty(userChoseNumber))
                throw new ArgumentNullException("userChoseNumber", "参数不能为空");
            if (base._RegistNum(userChoseNumber) == null)
                throw new ArgumentException("参数格式不正确:" + userChoseNumber, "userChoseNumber");
            var count1 = 0;
            var count2 = 0;
            openCode.Split(',').Each(p =>
            {
                if (int.Parse(p) > 40)
                    count1++;
                else
                    count2++;
            });
            var str1 = count1>count2?"虎":count1==count2?"和":"龙";
            var winList = new List<string>();
            if (userChoseNumber.Contains(str1))
                winList.Add(str1);
            return winList;
        }


        protected override int _GetNumCount(string userChoseNumber)
        {
            if (string.IsNullOrEmpty(userChoseNumber))
                return 0;
            else
                return userChoseNumber.Length;
        }

        protected override List<string> _GetChoseNumberList(string userChoseNumber)
        {
            if (string.IsNullOrEmpty(userChoseNumber))
                return new List<string>();
            return userChoseNumber.Select(p => p.ToString()).ToList();
        }
    }
}
